package com.lgt_enterprise_android.view.pages.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lgt_enterprise_android.Constant;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.CommonUtil;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.utils.PrefUtils;
import com.lgt_enterprise_android.utils.StringUtil;
import com.lgt_enterprise_android.view.pages.LoginActivity;

import java.lang.ref.WeakReference;

public class EditPhoneNumberActivity extends BaseActivity {

    private TextView txtGetMsgCode, txtTip;
    private EditText edPhone, edMsgCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_phone_number);
        txtGetMsgCode = (TextView) findViewById(R.id.txt_get_msgcode);
        edPhone = (EditText) findViewById(R.id.ed_phone_no);
        edMsgCode = (EditText) findViewById(R.id.edit_val_code);
        txtTip = (TextView) findViewById(R.id.txt_tip);
        txtGetMsgCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String strPhone = edPhone.getText().toString();
                if (StringUtil.isEmpty(strPhone)) {
                    txtTip.setText("手机号不能为空");
                    return;
                }

                if (!CommonUtil.isPhoneNumValid(strPhone)) {
                    txtTip.setText("手机号格式不正确");
                    return;
                }

                HttpManager.sendmsgCodeByChangePhone(mContext, strPhone, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        showToast("验证码发送成功");
                        CommonUtil.startTimer(new WeakReference<>(txtGetMsgCode), "获取验证码", 60, 1);
                        edPhone.requestFocus();
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });


            }
        });

        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String strPhone = edPhone.getText().toString();
                if (StringUtil.isEmpty(strPhone)) {
                    txtTip.setText("手机号不能为空");
                    return;
                }

                if (!CommonUtil.isPhoneNumValid(strPhone)) {
                    txtTip.setText("手机号格式不正确");
                    return;
                }

                String strMsgCode = edMsgCode.getText().toString();

                if (StringUtil.isEmpty(strMsgCode)) {
                    txtTip.setText("验证码不能为空");
                    return;
                }

                showLoadingDialog();

                HttpManager.editPhone(mContext, strPhone, strMsgCode, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        dismissLoadingDialog();
                        showToast("手机号码修改成功");
                        Intent intent = new Intent(mContext, LoginActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        Constant.token = "";
                        PrefUtils.setSharedPrefStringData(mContext, PrefUtils.TOKEN, "");
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onFailure(String mes) {
                        dismissLoadingDialog();
                    }
                });
            }
        });
    }
}
